<template>
  <ContentWrap>
    <el-form :model="queryParams" ref="queryFormRef" label-width="auto" :inline="true">
      <el-form-item label="计划编号" prop="planNo">
        <el-input
          v-model="queryParams.planNo"
          placeholder="请输入计划编号"
          :clearable="true"
          @keyup.enter="getPage"
        />
      </el-form-item>
      <el-form-item label="计划名称" prop="planName">
        <el-input
          v-model="queryParams.planName"
          placeholder="请输入计划名称"
          :clearable="true"
          @keyup.enter="getPage"
        />
      </el-form-item>
      <el-form-item label="计划状态" prop="planStatus">
        <el-select v-model="queryParams.planStatus" placeholder="请输入计划状态" :clearable="true">
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.RESTAURANT_PLAN_STATUS)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="餐饮套餐" prop="comboId">
        <el-select v-model="queryParams.comboId" placeholder="请输入餐饮套餐" :clearable="true">
          <el-option
            v-for="dict in comboList"
            :key="dict.id"
            :label="dict.comboName"
            :value="dict.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button v-hasPermi="['restaurant:plan:query']" @click="handleClickSearch">
          <Icon icon="ep:search" />
          搜索
        </el-button>
        <el-button @click="handleClickReset">
          <Icon icon="ep:refresh" />
          重置
        </el-button>
        <el-button v-hasPermi="['restaurant:plan:create']" @click="handleClickCreate">
          <Icon icon="ep:plus" />
          新增
        </el-button>
      </el-form-item>
    </el-form>
    <Table
      :columns="columns"
      :page-param="queryParams"
      :adaptive="true"
      :page-data="queryData"
      save-key="RestaurantPlan"
      height-per="90"
      @page-change="getPage"
      @row-dblclick="handleTableDblclick"
    >
      <template #menu="{ row }">
        <context-menu-item
          label="修改"
          @click="handleMenuUpdate(row)"
          v-hasPermi="['restaurant:plan:update']"
        />
        <context-menu-item
          label="删除"
          @click="handleMenuDeleted(row)"
          v-hasPermi="['restaurant:plan:deleted']"
        />
        <context-menu-item
          label="发布"
          @click="handleMenuPublish(row)"
          v-hasPermi="['restaurant:plan:publish']"
        />
        <context-menu-item
          label="驳回"
          @click="handleMenuRevert(row)"
          v-hasPermi="['restaurant:plan:revert']"
        />
      </template>
    </Table>
  </ContentWrap>
  <RestaurantPlanEdit ref="editRef" @success="getPage" />
</template>
<script setup name="RestaurantPlan" lang="ts">
import * as RestaurantRecipeApi from '@/api/restaurant/recipe'
import * as RestaurantPlanApi from '@/api/restaurant/plan'
import { DICT_TYPE, getDictObj, getIntDictOptions } from '@/utils/dict'
import RestaurantPlanEdit from './RestaurantPlanEdit.vue'
import { RestaurantPlanStatusEnum } from '@/utils/constants'
import { formatDate } from '@/utils/formatTime'
const message = useMessage() // 消息弹窗
const comboList = ref()
const queryFormRef = ref()
const editRef = ref()
const queryParams = ref({
  planNo: undefined,
  planName: undefined,
  planStatus: undefined,
  comboId: undefined
})
const queryData = ref()
const columns = [
  {
    label: '计划编号',
    prop: 'planNo'
  },
  {
    label: '计划名称',
    prop: 'planName'
  },
  {
    label: '开始时间',
    prop: 'beginTime',
    formatter: ({ beginTime }) => formatDate(beginTime, 'YYYY-MM-DD')
  },
  {
    label: '结束时间',
    prop: 'endTime',
    formatter: ({ endTime }) => formatDate(endTime, 'YYYY-MM-DD')
  },
  {
    label: '计划状态',
    prop: 'planStatus',
    formatter: ({ planStatus }) => getDictObj(DICT_TYPE.RESTAURANT_PLAN_STATUS, planStatus).label
  },
  {
    label: '套餐',
    prop: 'combo.comboName'
  },
  {
    label: '创建时间',
    prop: 'createTime'
  }
]
/** 初始化 */
onMounted(async () => {
  getPage()
})

/** 搜索按钮操作 */
const handleClickSearch = () => {
  getPage()
}

/** 重置按钮操作 */
const handleClickReset = () => {
  queryFormRef.value?.resetFields()
  handleClickSearch()
}

/** 新增按钮操作 */
const handleClickCreate = () => {
  editRef.value.open('create', 0)
}

/** 处理响应事件  */
const getPage = async () => {
  comboList.value = await RestaurantRecipeApi.listQueryCombo()
  queryData.value = await RestaurantPlanApi.query(queryParams.value)
}

/** 处理右击事件 修改 */
const handleMenuUpdate = (row) => {
  editRef.value.open('update', row.id)
}

/** 处理tables双击修改事件 */
const handleTableDblclick = (row) => {
  editRef.value.open('update', row.id)
}

/** 处理右击事件 删除 */
const handleMenuDeleted = async (row) => {
  await message.confirm('确定要删除此计划吗？')
  await RestaurantPlanApi.deleted(row.id)
  getPage()
}

/** 处理右击事件 发布 */
const handleMenuPublish = async (row) => {
  await RestaurantPlanApi.updateStatus({ id: row.id, planStatus: RestaurantPlanStatusEnum.PUBLISH })
  getPage()
}

/** 处理右击事件 驳回 */
const handleMenuRevert = async (row) => {
  await RestaurantPlanApi.updateStatus({
    id: row.id,
    planStatus: RestaurantPlanStatusEnum.UNPUBLISHED
  })
  getPage()
}
</script>
